package com.ruoyi.job.shortMessage;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.job.domain.*;
import com.ruoyi.job.mapper.*;
import com.ruoyi.job.task.CallProcdure;
import com.ruoyi.job.util.GetOnlyId;
import com.ruoyi.job.util.ShortMessageServiceSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Component("shortMessage")
public class ShortMessage {
    private Logger logger = LoggerFactory.getLogger(ShortMessage.class);
    @Autowired
    private DataCustDxtsMapper dataCustDxtsMapper;

    @Autowired
    private MessagePohneMapper messagePohneMapper;

    @Autowired
    private QmcyCustCkfxMapper qmcyCustCkfxMapper;

    @Autowired
    private QmcyCustDkfxMapper qmcyCustDkfxMapper;

    @Autowired
    private QmcyCustZjctMapper qmcyCustZjctMapper;

    @Autowired
    private QmcyCustKmbdMapper qmcyCustKmbdMapper;

    @Autowired
    private RedisService redisService;

    @Transactional
    public void smsSend() {
        logger.info("开始执行短信存储过程定时任务===================");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); //得到前一天
        Date date = calendar.getTime();
        String sd = new SimpleDateFormat("yyyyMMdd").format(date);
        StringBuffer authKey = new StringBuffer();
        authKey.append("jx-smsSend-").append(sd);

        if ("2".equals(redisService.getCacheObject(authKey.toString()))) {
            logger.info(">>>>>>>>>>当天业绩短信发送完成,直接返回");
            return;  //当天执行完成,直接返回
        }


        Date nowDate=new Date();
        SimpleDateFormat nowSdf=new SimpleDateFormat("yyyyMMdd");
        String currentday=nowSdf.format(new Date(nowDate.getTime()));
        String smsMsg = "proc-" + currentday + "-SHORT_MESSAGE";

        if (!"1".equals(redisService.getCacheObject(smsMsg))) {
            logger.info(">>>>>>>>>>业绩短信存储过程执行未完成!");
            return;
        }
        //获取发送的手机号码
        List<MessagePohne> messagePohneList = messagePohneMapper.selectMessagePohneList(new MessagePohne());
        String sdf = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //存款分析
        List<QmcyCustCkfx> qmcyCustCkfxList = qmcyCustCkfxMapper.selectQmcyCustCkfxList(new QmcyCustCkfx());
        for (QmcyCustCkfx qmcyCustCkfx : qmcyCustCkfxList) {
            StringBuffer shortMessage = new StringBuffer();
            String orgid = qmcyCustCkfx.getOrgId();
            shortMessage.append(sdf + ":[各项存款-"+orgid+"]\n");
//            shortMessage.append("存款本期(万元):" + qmcyCustCkfx.getBqYe() + "\n");
            shortMessage.append("存款本期(万元):" + qmcyCustCkfx.getKmCk() + "\n");
            shortMessage.append("存款较年初:" + qmcyCustCkfx.getJcnSs() + "\n");
            shortMessage.append("存款较前日:" + qmcyCustCkfx.getCkjqr() + "\n");
            shortMessage.append("较全年任务:" + qmcyCustCkfx.getJldJsRw() + "\n");
            shortMessage.append("本期储蓄存款:" + qmcyCustCkfx.getCxBqYe() + "\n");
            shortMessage.append("储蓄较年初:" + qmcyCustCkfx.getCxJncYe() + "\n");
            shortMessage.append("储蓄较昨日:" + qmcyCustCkfx.getCxJqr() + "\n");
            shortMessage.append("本期对公存款:" + qmcyCustCkfx.getDgBqYe() + "\n");
            shortMessage.append("对公较年初:" + qmcyCustCkfx.getDgJncYe() + "\n");
            shortMessage.append("对公较昨日:" + qmcyCustCkfx.getDgJqr() + "\n");

//            shortMessage.append(sdf + ":[存款分析-"+orgid+"]\n");
//            shortMessage.append("本期(万元):" + qmcyCustCkfx.getBqYe() + "\n");
//            shortMessage.append("较年初:" + qmcyCustCkfx.getJcnSs() + "\n");
//            shortMessage.append("较劳动竞赛任务:" + qmcyCustCkfx.getJldJsRw() + "\n");
//            shortMessage.append("本期储蓄存款:" + qmcyCustCkfx.getCxBqYe() + "\n");
//            shortMessage.append("较年初:" + qmcyCustCkfx.getCxJncYe() + "\n");
//            shortMessage.append("本期对公存款:" + qmcyCustCkfx.getDgBqYe() + "\n");
//            shortMessage.append("较年初:" + qmcyCustCkfx.getDgJncYe() + "\n");
//            shortMessage.append("本期卡存款:" + qmcyCustCkfx.getYhkBqYe() + "\n");
//            shortMessage.append("本期社保卡存款:" + qmcyCustCkfx.getSbkYe() + "\n");
            String sms = shortMessage.toString();
            if ("320826999".equals(orgid)) {
//                List<MessagePohne> messagePohneList = messagePohneMapper.selectMessagePohneList(new MessagePohne());
                for (MessagePohne messagePohne : messagePohneList) {
                    String phone = messagePohne.getPhone();
                    logger.info(">>>>>>>>>>当前执行:"+orgid+",phone:"+phone);
                    ifMessageSuccess(phone, sms);
//                    if(phone.equals("13739186036")){
//                        ifMessageSuccess(phone, sms);
//                    }

                }
            } else {
                List<String> getUserPhone = qmcyCustCkfxMapper.getUserPhone(orgid);
                for(String phone :getUserPhone){
                    logger.info(">>>>>>>>>>当前执行:"+orgid+",phone:"+phone);
                    ifMessageSuccess(phone, sms);
//                    if(phone.equals("13739186036")){
//                        ifMessageSuccess(phone, sms);
//                    }
                }
            }

        }

        //贷款分析
        List<QmcyCustDkfx> qmcyCustDkfxList = qmcyCustDkfxMapper.selectQmcyCustDkfxList(new QmcyCustDkfx());
        for (QmcyCustDkfx qmcyCustDkfx : qmcyCustDkfxList) {
            StringBuffer shortMessage = new StringBuffer();
            String orgid = qmcyCustDkfx.getOrgId();

            shortMessage.append(sdf + ":[各项贷款-"+orgid+"]\n");
//            shortMessage.append("贷款本期(万元):" + qmcyCustDkfx.getBqYe() + "\n");
            shortMessage.append("贷款本期(万元):" + qmcyCustDkfx.getKmDk() + "\n");
            shortMessage.append("贷款较年初:" + qmcyCustDkfx.getJncSs() + "\n");
            shortMessage.append("贷款较前日:" + qmcyCustDkfx.getDkjqr() + "\n");
            shortMessage.append("较全年任务:" + qmcyCustDkfx.getjQnRw() + "\n");
            shortMessage.append("其中-票据本期:" + qmcyCustDkfx.getPjYe() + "\n");
            shortMessage.append("票据较年初:" + qmcyCustDkfx.getPjNcYe() + "\n");
            shortMessage.append("票据较昨日:" + qmcyCustDkfx.getPjQrye() + "\n");
//            shortMessage.append(sdf + ":[贷款分析-"+orgid+"]\n");
//            shortMessage.append("本期(万元):" + qmcyCustDkfx.getBqYe() + "\n");
//            shortMessage.append("较年初:" + qmcyCustDkfx.getJncSs() + "\n");
//            shortMessage.append("较全年考核任务:" + qmcyCustDkfx.getjQnRw() + "\n");
//            shortMessage.append("贴现:" + qmcyCustDkfx.getTx() + "\n");
//            shortMessage.append("转贴现负债:" + qmcyCustDkfx.getzTxFz() + "\n");
//            shortMessage.append("当年到期金额:" + qmcyCustDkfx.getDnDqJe() + "\n");
//            shortMessage.append("当年到期笔数:" + qmcyCustDkfx.getDnDqBs() + "\n");
//            shortMessage.append("其中:昨日到期金额:" + qmcyCustDkfx.getYqJe() + "\n");
//            shortMessage.append("其中:昨日到期笔数:" + qmcyCustDkfx.getYqBs() + "\n");
            String sms = shortMessage.toString();
            if ("320826999".equals(orgid)) {
//                List<MessagePohne> messagePohneList = messagePohneMapper.selectMessagePohneList(new MessagePohne());
                for (MessagePohne messagePohne : messagePohneList) {
                    String phone = messagePohne.getPhone();
                    logger.info(">>>>>>>>>>当前执行:"+orgid+",phone:"+phone);
                    ifMessageSuccess(phone, sms);
//                    if(phone.equals("13739186036")){
//                        ifMessageSuccess(phone, sms);
//                    }
                }
            } else {
                List<String> getUserPhone = qmcyCustCkfxMapper.getUserPhone(orgid);
                for(String phone :getUserPhone){
                    logger.info(">>>>>>>>>>当前执行:"+orgid+",phone:"+phone);
                    ifMessageSuccess(phone, sms);
//                    if(phone.equals("13739186036")){
//                        ifMessageSuccess(phone, sms);
//                    }
                }
            }
        }
        //科目变动
//        List<QmcyCustKmbd> qmcyCustKmbdList = qmcyCustKmbdMapper.selectQmcyCustKmbdList(new QmcyCustKmbd());
//        for (QmcyCustKmbd qmcyCustKmbd : qmcyCustKmbdList) {
//            StringBuffer shortMessage = new StringBuffer();
//            String orgid = qmcyCustKmbd.getCmOpunCod();
//            shortMessage.append(sdf + ":[科目变动-"+orgid+"]\n");
//            shortMessage.append("现金:" + qmcyCustKmbd.getXj() + "\n");
//            shortMessage.append("较前日:" + qmcyCustKmbd.getXjjqr() + "\n");
//            shortMessage.append("行内清算款项:" + qmcyCustKmbd.getHnqs() + "\n");
//            shortMessage.append("较前日:" + qmcyCustKmbd.getHnqsjqr() + "\n");
//            shortMessage.append("各项存款:" + qmcyCustKmbd.getCk() + "\n");
//            shortMessage.append("较前日:" + qmcyCustKmbd.getCkjqr() + "\n");
//            shortMessage.append("各项贷款:" + qmcyCustKmbd.getDk() + "\n");
//            shortMessage.append("较前日:" + qmcyCustKmbd.getDkjqr() + "\n");
//            shortMessage.append("应解汇款:" + qmcyCustKmbd.getYjhk() + "\n");
//            shortMessage.append("较前日:" + qmcyCustKmbd.getYjhkjqr() + "\n");
//            shortMessage.append("应收息:" + qmcyCustKmbd.getYslx() + "\n");
//            shortMessage.append("较前日:" + qmcyCustKmbd.getYslxjqr());
//            String sms = shortMessage.toString();
//
//
//            if ("320826999".equals(orgid)) {
//                List<MessagePohne> messagePohneList = messagePohneMapper.selectMessagePohneList(new MessagePohne());
//                for (MessagePohne messagePohne : messagePohneList) {
//                    String phone = messagePohne.getPhone();
//                    logger.info(">>>>>>>>>>当前执行:"+orgid+",phone:"+phone);
//                    ifMessageSuccess(phone, sms);
//                }
//            } else {
//                List<String> getUserPhone = qmcyCustCkfxMapper.getUserPhone(orgid);
//                for(String phone :getUserPhone){
//                    logger.info(">>>>>>>>>>当前执行:"+orgid+",phone:"+phone);
//                    ifMessageSuccess(phone, sms);
//                }
//            }
//        }

        redisService.setCacheObject(authKey.toString(), "2"); //当天执行完成
        logger.info(">>>>>>>>>>当天业绩短信执行完成");

//        List<QmcyCustZjct> qmcyCustZjctList = qmcyCustZjctMapper.selectQmcyCustZjctList(new QmcyCustZjct());
//        for(QmcyCustZjct qmcyCustZjct:qmcyCustZjctList){
//            sjrq = qmcyCustZjct.getXfsjc();
//            if(sjrq == null || !sjrq.equals(sd)){
//                return;
//            }
//        }


        //获取资金寸头
//        shortMessage = new StringBuffer();
//        shortMessage.append(sdf + ":[资金头寸情况表-总行]\n" );
//        for(QmcyCustZjct qmcyCustZjct:qmcyCustZjctList){
//            shortMessage.append(qmcyCustZjct.getGlAcctName()+":"+qmcyCustZjct.getYxye() +"\n");
//        }
//        String zjtc = shortMessage.toString().trim();
//        msgs.add(zjtc.substring(0,zjtc.length()-1));
//        msgs.add(shortMessage.toString().trim());




    }

    public boolean ifMessageSuccess(String phone, String msg) {
        String status = "0";
        Map<String, Object> result = new ShortMessageServiceSupport().sendShortMessage(phone, msg);
        boolean flag = (boolean) result.get("success");
        if (flag) {
            status = "1";
        }
        DataCustDxts dataCustDxts = new DataCustDxts();
        String id = GetOnlyId.getOnlyId();
        dataCustDxts.setId(id);
        dataCustDxts.setDxnr(msg);
        dataCustDxts.setKhqt("JX");
        dataCustDxts.setStatus(status);
        dataCustDxts.setZhUserPhone(phone);
//        dataCustDxts.setZhUserId(messagePohne.getUserId());
//        dataCustDxts.setZhUserName(messagePohne.getName());
        dataCustDxtsMapper.insertDataCustDxts(dataCustDxts);
        return flag;
    }
}
